﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LanguageInfo.Factory;
using LanguageInfo.IDAL;
using System.Data;
using LanguageInfo.Common;
using LanguageInfo.Common.Utilities;
using System.Transactions;
using System.Text.RegularExpressions;

namespace LanguageInfo.BLL
{
    public class CorporaBLL
    {
        ICorporaDAL narInfoDAL = LanguageInfoFactory.CreateCorporaDAL();
       
        public string GetCorporaInfo(string strInfo, int PageSize, int pageNo)
        {
            string strJosn = string.Empty;
            try
            {
                int startIndex = 0;
                int endIndex = 0;
                int start = 0;
                if (pageNo > 0)
                    start = (pageNo - 1) * PageSize;
                startIndex = start;
                endIndex = start + PageSize;
                FlexigridObject objJosn = new FlexigridObject();
                DataSet ds = narInfoDAL.GetCorporaInfo(strInfo, startIndex, endIndex);
                if (ds != null && ds.Tables.Count > 1)
                {
                    int intTotal = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
                    objJosn.total = intTotal;
                    objJosn.page = pageNo;
                    
                    DataTable dtInfo = ds.Tables[0];
                    List<FlexigridRow> objListRow = new List<FlexigridRow>();
                    for (int i = 0; i < dtInfo.Rows.Count; i++)
                    {
                        FlexigridRow objRow = new FlexigridRow();
                        objRow.id = dtInfo.Rows[i]["SentenceId"].ToString();
                        List<string> objListStr = new List<string>();
                        objListStr.Add(objRow.id);
                        objListStr.Add(Convert.ToString(start + i + 1));
                        string strSentenceEn = Regex.Replace(dtInfo.Rows[i]["SentenceEn"].ToString(), strInfo, "<font color=\"#FF0000\">" + strInfo + "</font>", RegexOptions.IgnoreCase);
                        strSentenceEn += "<br />" + dtInfo.Rows[i]["SentenceCh"].ToString();
                        objListStr.Add(strSentenceEn);
                        //objListStr.Add(dtInfo.Rows[i]["SentenceCh"].ToString());
                        //objListStr.Add(dtInfo.Rows[i]["CreateTime"].ToString());
                        objRow.cell = objListStr;
                        objListRow.Add(objRow);
                    }
                    objJosn.rows = objListRow;
                    strJosn = JsonHelper.Serializer(objJosn);
                }
                return strJosn;
            }
            catch (Exception ex)
            {
                LoginErr.log(ex);
                throw;
            }
        }

        public string GetComCorporaInfo(string txtAllWords, 
            string txtFullWords, string txtAllMatched, string txtNoMatched, int PageSize, int pageNo)
        {
            string strJosn = string.Empty;
            try
            {
                int startIndex = 0;
                int endIndex = 0;
                int start = 0;
                if (pageNo > 0)
                    start = (pageNo - 1) * PageSize;
                startIndex = start;
                endIndex = start + PageSize;
                FlexigridObject objJosn = new FlexigridObject();
                DataSet ds = narInfoDAL.GetCorporaInfo(txtAllWords, startIndex, endIndex);//xiugai 
                if (ds != null && ds.Tables.Count > 1)
                {
                    int intTotal = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
                    objJosn.total = intTotal;
                    objJosn.page = pageNo;

                    DataTable dtInfo = ds.Tables[0];
                    List<FlexigridRow> objListRow = new List<FlexigridRow>();
                    for (int i = 0; i < dtInfo.Rows.Count; i++)
                    {
                        FlexigridRow objRow = new FlexigridRow();
                        objRow.id = dtInfo.Rows[i]["SentenceId"].ToString();
                        List<string> objListStr = new List<string>();
                        objListStr.Add(objRow.id);
                        objListStr.Add(Convert.ToString(start + i + 1));
                        string strSentenceEn = Regex.Replace(dtInfo.Rows[i]["SentenceEn"].ToString(), txtAllWords, "<font color=\"#FF0000\">" + txtAllWords + "</font>", RegexOptions.IgnoreCase);
                        strSentenceEn += "<br />" + dtInfo.Rows[i]["SentenceCh"].ToString();
                        objListStr.Add(strSentenceEn);
                        //objListStr.Add(dtInfo.Rows[i]["SentenceCh"].ToString());
                        //objListStr.Add(dtInfo.Rows[i]["CreateTime"].ToString());
                        objRow.cell = objListStr;
                        objListRow.Add(objRow);
                    }
                    objJosn.rows = objListRow;
                    strJosn = JsonHelper.Serializer(objJosn);
                }
                return strJosn;
            }
            catch (Exception ex)
            {
                LoginErr.log(ex);
                throw;
            }
        }

        public string GetStaristicsCorporaInfo(int PageSize, int pageNo)
        {
            string strJosn = string.Empty;
            try
            {
                int startIndex = 0;
                int endIndex = 0;
                int start = 0;
                if (pageNo > 0)
                    start = (pageNo - 1) * PageSize;
                startIndex = start;
                endIndex = start + PageSize;
                FlexigridObject objJosn = new FlexigridObject();
                DataSet ds = narInfoDAL.GetCorporaInfoByUserName();
                if (ds != null && ds.Tables.Count > 0)
                {
                    objJosn.total = 10000;
                    objJosn.page = pageNo;

                    DataTable dtInfo = ds.Tables[0];
                    List<FlexigridRow> objListRow = new List<FlexigridRow>();
                    for (int i = 0; i < dtInfo.Rows.Count; i++)
                    {
                        FlexigridRow objRow = new FlexigridRow();
                        objRow.id = Convert.ToString(start + i + 1);
                        List<string> objListStr = new List<string>();
                        objListStr.Add(objRow.id);
                        objListStr.Add(dtInfo.Rows[i]["StatisType"].ToString());
                        objListStr.Add(dtInfo.Rows[i]["StatisTypeNum"].ToString());
                        objListStr.Add(dtInfo.Rows[i]["StatisSize"].ToString());
                        objRow.cell = objListStr;
                        objListRow.Add(objRow);
                    }
                    objJosn.rows = objListRow;
                    strJosn = JsonHelper.Serializer(objJosn);
                }
                return strJosn;
            }
            catch (Exception ex)
            {
                LoginErr.log(ex);
                throw;
            }
        }

        public void DelCorporaArry(string[] _SentenceIdList)
        {
            try
            {
                using (TransactionScope trans = new TransactionScope())
                {
                    for (int i = 0; i < _SentenceIdList.Length; i++)
                    {
                        narInfoDAL.DelCorpora(_SentenceIdList[i]);
                    }
                    trans.Complete();
                }
            }
            catch (Exception ex)
            {
                LoginErr.log(ex);
                throw;
            }
        }

        /// <summary>
        /// 批量新增句子
        /// </summary>
        /// <param name="AddTableds"></param>
        public void InsertResolveiSentenceInfo(DataSet AddTableds)
        {
            try
            {
                narInfoDAL.InsertResolveiSentenceInfo(AddTableds);
            }
            catch (Exception ex)
            {
                LoginErr.log(ex);
                throw;
            }
        }
        public DataSet GetCorporaInfoById(string strId)
        {
            try
            {
                return narInfoDAL.GetCorporaInfoById(strId);
            }
            catch (Exception ex)
            {
                LoginErr.log(ex);
                throw;
            }
        }

        /// <summary>
        /// 批量新增句子
        /// </summary>
        /// <param name="AddTableds"></param>
        public void UpdateResolveiSentenceInfo(DataSet AddTableds)
        {
            try
            {
                narInfoDAL.UpdateResolveiSentenceInfo(AddTableds);
            }
            catch (Exception ex)
            {
                LoginErr.log(ex);
                throw;
            }
        }
    }
}
